<?php


namespace app\api\controller\v38;


use app\api\controller\v2\Auth;
use app\common\cachemodel\chat\HzQuestion;
use app\common\cachemodel\chat\HzQuestionText;
use app\common\cachemodel\Recode as RecodeModel;
use app\common\cachemodel\zl5\Zl5ExplainChat;
use app\common\cachemodel\zl5\Zl5ExplainContent;
use app\common\cachemodel\zl5\Zl5ExplainTitle;
use app\common\cachemodel\zl5\Zl5ExplainType;
use app\common\lib\Util;
use think\Db;

class NatalChat extends Auth
{
    /**
     * 提问
     */
    public function askQuestions()
    {
        $qid = input('qid/d', 0);
        $rid = input('rid/d', 0);

        $recodeInfo = (new RecodeModel())->getInfoById($rid);
        if (!$recodeInfo || $recodeInfo['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '请求异常，档案不存在～');
        }

        $zl5ExplainTitleModel = new Zl5ExplainTitle();
        $questionInfo = $zl5ExplainTitleModel->getInfoById($qid);
        if (!$questionInfo)
        {
            return show(config('code.error'), '服务异常，请稍后再试～');
        }
        // 去结果的 依据
        $basis = Util::convertNatalBasisRes($recodeInfo, $questionInfo['basis_easy']);

        $zl5ExplainContentModel = new Zl5ExplainContent();
        $contentInfo = $zl5ExplainContentModel->getExplainContentInfo($qid, $basis);

        $insertData = [
            'status' => 1,
            'uid' => $this->user['id'],
            'rid' => $rid,
            'type_id' => $questionInfo['type_id'],
            'title_id' => $qid,
            'content_id' => $contentInfo['id'],
            'state' => 0,
            'create_time' => $this->request->time(),
        ];
        $chatId = (new Zl5ExplainChat())->SaveData($insertData);

        $zl5ExplainTitleModel->where(['id' => $qid])->setInc('question_num');

        $typeInfo = (new Zl5ExplainType())->getInfoById($questionInfo['type_id']);

        $linkList = [
            [
                'type' => 'basis',
                'title' => $zl5ExplainContentModel->convertBasisToText($basis),
                'basis' => $basis,
                'target' => '',
                'link' => '',
            ]
        ];
        if (isset($questionInfo['extend']['recommend_goods']))
        {
            foreach ($questionInfo['extend']['recommend_goods'] as $recommendGoodtem)
            {
                $linkList[] = [
                    'type' => 'shop',
                    'title' => $recommendGoodtem['title'],
                    'basis' => '',
                    'target' => $recommendGoodtem['target'],
                    'link' => $recommendGoodtem['link'],
                ];
            }
        }


        $resData = [
            'chat_id' => $chatId,
            'title' => $typeInfo['title'],
            'question' => $questionInfo['title'],
            'content' => $contentInfo['content'],
            'link_list' => $linkList,
            'state' => 0,
            'create_time' => '刚刚',
        ];

        return show(config('code.success'), '请求成功', $resData);
    }

    /**
     * 答案点赞与踩
     */
    public function answerLike()
    {
        $chatId = input('chat_id/d', 0);
        $state = input('state/d', 0);
        if (!in_array($state, [0, 1, 2], true))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }

        $zl5ExplainChatModel = new Zl5ExplainChat();
        $data = $zl5ExplainChatModel->getInfoById($chatId);
        if (!$data || $data['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '请求异常，数据不存在～');
        }

        if ($state === $data['state'])
        {
            return show(config('code.success'), '请求成功');
        }

        $res = $zl5ExplainChatModel->UpdateData($chatId, [
            'state' => $state,
            'update_time' => $this->request->time()
        ]);

        if ($res)
        {
            if ($data['qtype'] === 1)
            {
                $zl5ExplainContentModel = new Zl5ExplainContent();
                switch ($state)
                {
                    case 0:
                        if ($data['state'] === 1)
                        {
                            $zl5ExplainContentModel->where(['id' => $data['content_id']])->dec('zan_num')->update();
                        } else
                        {
                            $zl5ExplainContentModel->where(['id' => $data['content_id']])->dec('dislike_num')->update();
                        }
                        break;
                    case 1:
                        if ($data['state'] === 0)
                        {   // 未踩过
                            $zl5ExplainContentModel->where(['id' => $data['content_id']])->inc('zan_num')->update();
                        } else
                        {   // 踩过
                            $zl5ExplainContentModel->where(['id' => $data['content_id']])->inc('zan_num')->dec('dislike_num')->update();
                        }
                        break;
                    case 2:
                        if ($data['state'] === 1)
                        {   // 点赞过
                            $zl5ExplainContentModel->where(['id' => $data['content_id']])->dec('zan_num')->inc('dislike_num')->update();
                        } else
                        {   // 未点赞过
                            $zl5ExplainContentModel->where(['id' => $data['content_id']])->inc('dislike_num')->update();
                        }
                        break;
                    default:
                }
            } else
            {
                $updateData = [];
                $oldZan = $data['state'];
                if ($oldZan === 1)
                {
                    $updateData['zan'] = Db::raw('zan - 1');
                }
                if ($oldZan === 2)
                {
                    $updateData['zan1'] = Db::raw('zan1 - 1');
                }
                if ($state === 1)
                {
                    $updateData['zan'] = Db::raw('zan + 1');
                }
                if ($state === 2)
                {
                    $updateData['zan1'] = Db::raw('zan1 + 1');
                }

                // 更新 问题信息 点赞
                (new HzQuestion())->where('id', $data['title_id'])->update($updateData);
                // 更新 词条 点赞信息
                foreach ($data['extend']['result_data'] as $textId)
                {
                    (new HzQuestionText())->where('id', $textId)->update($updateData);
                }
            }
            return show(config('code.success'), '请求成功');
        } else
        {
            return show(config('code.error'), '服务异常，请稍后再试～');
        }
    }

    /**
     * 获取提问记录
     */
    public function getQuestionRecode()
    {
        $rid = input('rid/d', 0);
        $minId = input('min_id/d', 0);

        $resData = (new Zl5ExplainChat())->getPageDatas($rid, $minId);

        return show(config('code.success'), '请求成功', $resData);
    }
}
